//
//  CSSWelcomeViewController.swift
//  weibo28
//
//  Created by chushushan on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage


class CSSWelcomeViewController: UIViewController {

    //背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //设置头像
    private lazy var headImageView: UIImageView = {
    
        let headView = UIImageView(image: UIImage(named: "avatar_default_big"))
        headView.layer.cornerRadius = 45
        headView.layer.masksToBounds = true
        
        if let url = CSSUserAccountViewModel.shareUserAccount.userAccount?.avatar_large {
        
            headView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        return headView
    
    }()
    
    //欢迎信息
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        
        if let name = CSSUserAccountViewModel.shareUserAccount.userAccount?.name {
        
        label.text = name + "欢迎回来"
        
        } else {
        
        label.text = "欢迎回来"
        }
      
        label.font = UIFont.systemFontOfSize(14)

        return label
    }()
    
    
    
    override func loadView() {
        //直接把背景Imageview给view
        view = bgImageView
    }
    
    
    override func viewDidAppear(animated: Bool) {
        
        startAnimation()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

       setupUI()
    }

  
    //添加子控件
    private func setupUI() {
    
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        //设置约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(10)
        }
    }
    
    
    //添加动画
    private func startAnimation() {
    
        //改变约束
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
            
            }
        messageLabel.alpha = 0
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //让子视图重新布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        //进入首页
                    //发送通知切换根视图控制区
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
                
        }
        
      
     
    }
    
    
    
    
    

}
